package com.xianyun.book.convertor;

import com.xianyun.book.domain.book.Book;
import com.xianyun.book.domain.book.BookStatus;
import com.xianyun.book.dto.clientobject.BookInfoCO;
import com.xianyun.book.gatewayimpl.database.dataobject.BookDO;
import com.xianyun.book.gatewayimpl.elasticsearch.dataobject.BookEsDO;
import org.springframework.cglib.beans.BeanCopier;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static java.util.stream.Collectors.toList;

/**
 * @author mac_zyj
 */
public class BookConvertor {

    /**书籍转换复制器*/
    private static final BeanCopier BOOK_COPIER = BeanCopier.create(BookDO.class, BookInfoCO.class,false);

    public static BookInfoCO toDataCo(BookDO bookDO){
        return getBookInfoCO(bookDO);
    }

    public static BookInfoCO toDataCo(Book book){
        BookInfoCO obj = new BookInfoCO();
        obj.setAuthor(book.getAuthor());
        obj.setName(book.getBookName());
        obj.setPrice(book.getPrice());
        obj.setBookStatus(book.getBookStatus().getMessage());
        obj.setPress(book.getPress());
        obj.setImgUrl(book.getImgUrl());
        obj.setDesc(book.getDesc());
        obj.setIsbn(book.getIsbn());
        obj.setPersonNumber(book.getPersonNumber());
        obj.setScore(book.getScore());
        obj.setInSellNumber(book.countInSellerNumber());
        obj.setYears(book.getPublishDate());
        obj.setNewTime(book.getNewTime());
        return obj;
    }

    private static BookInfoCO getBookInfoCO(BookDO bookDO) {
        BookInfoCO obj = new BookInfoCO();
        obj.setAuthor(bookDO.getAuthor());
        obj.setName(bookDO.getBookName());
        obj.setPrice(bookDO.getOriginalPrice());
        obj.setBookStatus(BookStatus.getStatusByNumber(bookDO.getBookStatus()).getMessage());
        obj.setPress(bookDO.getPress());
        obj.setImgUrl(bookDO.getImgUrl());
        obj.setDesc(bookDO.getBookDesc());
        obj.setIsbn(bookDO.getIsbn());
        obj.setPersonNumber(bookDO.getPersonNumber());
        obj.setScore(bookDO.getScore());
        obj.setInSellNumber(bookDO.getInStock());
        obj.setYears(bookDO.getPublishDate());
        obj.setNewTime(bookDO.getNewTime());
        return obj;
    }

    private static BookInfoCO initBookEsToCo(BookEsDO bookEsDO){
        BookInfoCO obj = new BookInfoCO();
        obj.setAuthor(bookEsDO.getAuthor());
        obj.setName(bookEsDO.getTitle());
        obj.setBookStatus(BookStatus.PRE_SALE.getMessage());
        obj.setPress(bookEsDO.getPublish());
        obj.setImgUrl(bookEsDO.getImg());
        obj.setDesc(bookEsDO.getBrief());
        obj.setIsbn(bookEsDO.getIsbn());
        obj.setPersonNumber(bookEsDO.getLike_person());
        obj.setInSellNumber(0);
        obj.setYears(bookEsDO.getPublish_time());
        return obj;
    }

    public static List<BookInfoCO> toDataTranslateList(List<BookDO> resultDate){
        if (resultDate.size()>0){
            return resultDate.stream().map(BookConvertor::getBookInfoCO).collect(toList());
        }
        return new ArrayList<>();
    }

    public static List<BookInfoCO> toDataShowList(List<BookEsDO> resultDate){
        if (resultDate.size()>0){
            return resultDate.stream().map(BookConvertor::initBookEsToCo).collect(toList());
        }
        return new ArrayList<>();
    }

    public static BookDO toDateObject(Book book){
        BookDO bookDO = new BookDO();
        bookDO.setBookName(book.getBookName());
        bookDO.setIsbn(book.getIsbn());
        bookDO.setOriginalPrice(book.getPrice());
        bookDO.setBookDesc(book.getDesc());
        bookDO.setAuthor(book.getAuthor());
        bookDO.setImgUrl( book.getImgUrl());
        bookDO.setNewTime(book.getNewTime());
        bookDO.setPress(book.getPress());
        bookDO.setBookId(book.getBookId());
        bookDO.setScore(book.getScore());
        bookDO.setPersonNumber(book.getPersonNumber());
        bookDO.setBookStatus(book.getBookStatus().getStatusId());
        bookDO.setPublishDate(book.getPublishDate());
        bookDO.setDiscountRate(book.getDiscountRate());
        bookDO.setInStock(book.getInStock());
        bookDO.setModifyTime(new Date());
        return bookDO;
    }

    public static Book toDomainObject(BookDO bookDO){
        Book book =  new Book();
        book.setBookName(bookDO.getBookName());
        book.setIsbn(bookDO.getIsbn());
        book.setPrice(bookDO.getOriginalPrice());
        book.setDesc(bookDO.getBookDesc());
        book.setImgUrl(bookDO.getImgUrl());
        book.setAuthor(bookDO.getAuthor());
        book.setPress(bookDO.getPress());
        book.setPersonNumber(bookDO.getPersonNumber());
        book.setScore(bookDO.getScore());
        book.setNewTime(bookDO.getNewTime());
        book.setBookId(bookDO.getBookId());
        book.setInStock(bookDO.getInStock());
        book.setBookStatus(BookStatus.getStatusByNumber(bookDO.getBookStatus()));
        book.setPublishDate(bookDO.getPublishDate());
        book.setDiscountRate(book.getDiscountRate());
        return book;
    }
}
